﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace Utils
{
    public class HttpPost
    {
        private HttpWebRequest req;
        private string url;
        private string jsonParam;

        public HttpPost(string _url, string _param)
        {
            this.url = _url;
            this.jsonParam = _param;
        }
        public void get(Action<string,Exception> action)
        {
            req = (HttpWebRequest)WebRequest.Create(this.url);
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            req.ContentType = "application/json";
            req.Method = "GET";
            req.AllowWriteStreamBuffering = true;
            req.AllowAutoRedirect = true;
            req.KeepAlive = true;
            req.Pipelined = true;
            req.Timeout = 20 * 1000;
            req.BeginGetResponse(resp =>
            {
                try
                {
                    Stream responseStream = req.EndGetResponse(resp).GetResponseStream();
                    string responseString = String.Empty;
                    StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
                    responseString = sr.ReadToEnd();
                    sr.Close();
                    action(responseString, null);
                }
                catch (Exception ex)
                {
                    action(null, ex);
                }
            }, null);
        }

        public void post(Action<string, Exception> action)
        {
            byte[] bytesToSend = Encoding.UTF8.GetBytes(this.jsonParam);
            req = (HttpWebRequest)WebRequest.Create(this.url);
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            req.ContentType = "application/json";
            req.Method = "POST";
            req.AllowWriteStreamBuffering = true;
            req.AllowAutoRedirect = true;
            req.KeepAlive = true;
            req.Pipelined = true;
            req.ContentLength = bytesToSend.Length;
            req.BeginGetRequestStream(x =>
                {
                    try
                    {
                        Stream stream = req.EndGetRequestStream(x);
                        stream.BeginWrite(bytesToSend, 0, bytesToSend.Length, sx =>
                            {
                                stream.EndWrite(sx);
                                stream.Close();

                                req.BeginGetResponse(resp =>
                                    {
                                        try
                                        {
                                            Stream responseStream = req.EndGetResponse(resp).GetResponseStream();
                                            string responseString = String.Empty;
                                            StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
                                            responseString = sr.ReadToEnd();
                                            sr.Close();
                                            action(responseString, null);
                                        }
                                        catch(Exception ex)
                                        {
                                            action(null, ex);
                                        }
                                    }, null);

                            }, null);
                    }
                    catch(Exception ex)
                    {
                        action(null, ex);
                    }
                }, null);
        }

        public string post_sync()
        {
            Encoding encoding = Encoding.GetEncoding("utf-8");
            byte[] bytesToPost = Encoding.UTF8.GetBytes(jsonParam);
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
            httpRequest.ContentType = "application/json";
            httpRequest.Method = "POST";
            httpRequest.ContentLength = bytesToPost.Length;
            Stream requestStream = httpRequest.GetRequestStream();
            requestStream.Write(bytesToPost, 0, bytesToPost.Length);
            requestStream.Close();
            Stream responseStream = httpRequest.GetResponse().GetResponseStream();
            string stringResponse = string.Empty;
            StreamReader responseReader = new StreamReader(responseStream, Encoding.UTF8);
            stringResponse = responseReader.ReadToEnd();
            responseStream.Close();
            return stringResponse;
        }
    }
}
